package nero.yncc.com.createart.adapter;

import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;


import java.util.ArrayList;
import java.util.List;

import nero.yncc.com.createart.R;
import nero.yncc.com.createart.activity.ActivityAddressManager;
import nero.yncc.com.createart.bean.AddressBean;
import nero.yncc.com.createart.custom.IconView;
import nero.yncc.com.createart.utils.TextUtil;


/**
 * Created by Administrator.
 * Time:2017/3/18 10:13.
 */

public class AdapterAddress extends RecyclerView.Adapter<AdapterAddress.Holder> {

    Context context;
    ArrayList<AddressBean> arrayList;
    public AdapterAddress(ArrayList<AddressBean> data, Activity mContext, ActivityAddressManager.OnAddressItemClickListener listener) {
        this.listener = listener;
        this.arrayList=data;
        this.context=mContext;
    }

    private ActivityAddressManager.OnAddressItemClickListener listener;


    @Override
    public AdapterAddress.Holder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(context, R.layout.adapter_address, null);
        return new Holder(view);
    }

    @Override
    public void onBindViewHolder(Holder holder, int position) {
        final AddressBean item = arrayList.get(position);
        holder.tv_name.setText(TextUtil.getText(item.getConsignee(),""));
        holder.tv_phone.setText(item.getMobile());
        holder.tv_address.setText(item.getProvince() + " " +
                item.getCity() + " " +
                item.getDistrict() + " " +
                item.getAddress());
        holder.ll_default.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (listener != null) {
                    listener.onDefaultClick(item);
                }
                item.setIs_default("0");//0默认，1非默认
            }
        });
        holder.tv_edit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (listener != null) {
                    listener.onEditClick(item);
                }
            }
        });
        holder.tv_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (listener != null) {
                    listener.onDeleteClick(item);
                }
            }
        });
        if (item.getIs_default() .equals("1")) {
            holder.icon_default.setTextColor(context.getResources().getColor(R.color.project_red_low));
        } else {
            holder.icon_default.setTextColor(context.getResources().getColor(R.color.textColorGray));
        }

    }



    @Override
    public int getItemCount() {
        return arrayList.size();
    }

    class Holder extends RecyclerView.ViewHolder {
        TextView tv_name, tv_phone, tv_address, tv_delete, tv_edit;
        LinearLayout ll_default;
        IconView icon_default;

        public Holder(View itemView) {
            super(itemView);
            tv_name=itemView.findViewById(R.id.tv_name);
            tv_phone=itemView.findViewById(R.id.tv_phone);
            tv_address=itemView.findViewById(R.id.tv_address);
            tv_delete=itemView.findViewById(R.id.tv_delete);
            tv_edit=itemView.findViewById(R.id.tv_edit);
            ll_default=itemView.findViewById(R.id.ll_default);
            icon_default=itemView.findViewById(R.id.icon_default);
        }
    }

}
